#!/usr/bin/env python
#encoding: utf-8
import re
from subprocess import Popen, PIPE
import os,sys

'''
def getIfconfig():
    p = Popen(['ifconfig'], stdout = PIPE)
    data = p.stdout.read()
    return data
'''

def getDmi():
    p = Popen(['dmidecode'], stdout = PIPE)
    data = p.stdout.read()
    return data

def parseData(data):
    parsed_data = []
    new_line = ''
    data = [i for i in data.split('\n') if i]
    for line in data:
        if line[0].strip():
            parsed_data.append(new_line)
            new_line = line + '\n'
        else:
            new_line += line + '\n'
    parsed_data.append(new_line)
    return [i for i in parsed_data if i]

''' 根据输入的dmi段落数据 分析出指定参数 '''
def parseDmi(parsed_data):
    dic = {}
    parsed_data = [i for i in parsed_data if i.startswith('System Information')]
    parsed_data = [i for i in parsed_data[0].split('\n')[1:] if i]
    dmi_dic = dict([i.strip().split(':') for i in parsed_data])
    dic['Manufacturer'] = dmi_dic['Manufacturer'].strip()
    dic['UUID'] = dmi_dic['UUID'].strip()
    dic['sn'] = dmi_dic['Serial Number'].strip()
    return dic

def VirPhy(dic):
    vendor = ['VMware','QEMU','VMWARE','Xen']
    m = dic['Manufacturer']
    for i in vendor:
       rematch = re.search(i, m)
       if rematch:
          return 2
       else:
          return 1
    
#if __name__ == '__main__':
#    dic = {}
#    data_dmi = getDmi()
#    parsed_data_dmi = parseData(data_dmi)
#    dmi = parseDmi(parsed_data_dmi)
#    dic.update(dmi)
#    VirPhy(dic)

   # for k,v in dic.items():
   #     print '%-10s:%s' % (k, v)
